package ch2linkedlist;

import static ch2linkedlist.Chapter.*;

public class Ex2 {

	static ListNode nthToLast(ListNode head, int n) {		
		ListNode last = head;
		for (int i=0; i<n; i++) {
			last = last.next;
		}
		
		ListNode curr = head;
		while (last.next != null) {
			curr = curr.next;
			last = last.next;
		}
		return curr;
	}

	public static void main(String [] argv) {
		ListNode head = createSampleList(new int [] {0, 1, 2, 3});
		
		System.out.println("original: ");
		print(head);
		System.out.println("after: ");

		head = nthToLast(head, 2);
		print(head);
		
		
	}
	
}
